<?php if (!defined('BASEPATH')) exit('No direct script access allowed');
/**
 * This is a sample module for PyroCMS
 *
 * @author 		Jerel Unruh - PyroCMS Dev Team
 * @website		http://unruhdesigns.com
 * @package 	PyroCMS
 * @subpackage 	Sample Module
 */
class Admin extends Admin_Controller
{
	protected $section = 'listing_place_options';
	protected $_ci;
	public function __construct()
	{
		parent::__construct();
		$this->_ci = &get_instance();
		// Load all the required classes
		$this->load->library('form_validation');
		$this->load->model('escort_listing_place_options_m');
		$this->lang->load('listing_place');
		// Set the validation rules
		$this->validation_rules = array(
		array(
				'field' => 'country_id',
				'label' => 'lang:escort_listing_place_country_label',
				'rules' => 'trim|max_length[255]|required'
				),
				array(
				'field'	=> 'state_id',
				'label'	=> 'lang:escort_listing_place_state_label'
				),
				array(
				'field' => 'province_id',
				'label' => 'lang:escort_listing_place_province_label',
				'rules' => 'trim'
				),
				array(
				'field' => 'city_id',
				'label' => 'lang:escort_listing_place_city_label',
				'rules' => 'trim'
				),
				array(
				'field' => 'price',
				'label' => 'lang:escort_listing_place_price_label',
				'rules' => 'trim|required|numeric'
				)
				);
				$this->data->discountTypeList = array();

				$discountTypes = $this->_ci->discount_type_entity->getList();
				if($discountTypes){
					foreach ( $discountTypes as $discountType){
						$this->data->discountTypeList[$discountType->id] = $discountType->name;
					}
				}

				$this->template->set_partial('shortcuts', 'admin/partials/shortcuts')
				->append_metadata(js('escort.listing.place.js', 'be_listing_place'))
				->append_metadata(css('escort.listing.place.css', 'be_listing_place'));
	}

	/**
	 * List all items
	 */
	public function index()
	{
		// Create pagination links
		$total_rows = $this->escort_listing_place_options_m->count_all();
		$pagination = create_pagination('admin/be_listing_place/index', $total_rows);
		// Using this data, get the relevant results
		$listingPlaces = $this->escort_listing_place_options_m->limit($pagination['limit'])->get_all();
			
		if($listingPlaces){
			foreach ($listingPlaces as $listingPlace){
				$countryData = $this->_ci->countries_entity->init('id',$listingPlace->country_id);
				if($countryData){
					$listingPlace->countryName = $countryData->getCountryName();
				}else{
					$listingPlace->countryName = lang('escort_listing_place_all_country_label');
				}
				$listingPlace->stateName = lang('escort_listing_place_all_state_label');
				$listingPlace->provinceName = lang('escort_listing_place_all_province_label');
				$listingPlace->cityName = lang('escort_listing_place_all_city_label');
				if($countryData){
					if($countryData->getCountryCode() == "ES"){
						$stateData =  $this->_ci->states_spain_entity->init('id',$listingPlace->state_id);
						if($stateData){
							$listingPlace->stateName = $stateData->getRegionNameSpanish();
						}
						$provinceData = $this->provinces_spain_entity->init('id',$listingPlace->province_id);
						if($provinceData){
							$listingPlace->provinceName = $provinceData->getProvinceNameSpanish(); 
						}
						$cityData = $this->cities_spain_entity->init('id',$listingPlace->city_id);
						if($cityData){
							$listingPlace->cityName = $cityData->getCityNameSpanish(); 
						}
					}else{
						$stateData =  $this->_ci->states_entity->init('id',$listingPlace->state_id);
						if($stateData){
							$listingPlace->stateName = $stateData->getStateName();
						}
						$cityData = $this->cities_entity->init('id',$listingPlace->city_id);
						if($cityData){
							$listingPlace->cityName = $cityData->getCityName(); 
						}
					}
				}
			}
		}
		//$this->update();
		//do we need to unset the layout because the request is ajax?
		$this->input->is_ajax_request() ? $this->template->set_layout(FALSE) : '';

		$this->template
		->title($this->module_details['name'])
		->append_metadata(js('admin/filter.js'))
		->set('pagination', $pagination)
		->set('listingPlaces', $listingPlaces);

		$this->input->is_ajax_request() ? $this->template->build('admin/tables/posts', $this->data) : $this->template->build('admin/index', $this->data);
			
	}

	public function create()
	{
		$this->form_validation->set_rules($this->validation_rules);

		if ($this->form_validation->run())
		{
			$country_id	= $this->input->post('country_id');
			$state_id = $this->input->post('state_id');
			$province_id = $this->input->post('province_id');
			$city_id = $this->input->post('city_id');
			
			$countryData = $this->_ci->countries_entity->init('id',$country_id);
			if($countryData){
				$countryName = $countryData->getCountryName();
			}else{
				$countryName = NULL;
			}
			$stateName = NULL;
			$provinceName = NULL;
			$cityName = NULL;
			if($countryData){
				if($countryData->getCountryCode() == "ES"){
					$stateData =  $this->_ci->states_spain_entity->init('id',$state_id);
					if($stateData){
						$stateName = $stateData->getRegionNameSpanish();
					}
					$provinceData = $this->provinces_spain_entity->init('id',$province_id);
					if($provinceData){
						$provinceName = $provinceData->getProvinceNameSpanish(); 
					}
					$cityData = $this->cities_spain_entity->init('id',$city_id);
					if($cityData){
						$cityName = $cityData->getCityNameSpanish(); 
					}
				}else{
					$stateData =  $this->_ci->states_entity->init('id',$state_id);
					if($stateData){
						$stateName = $stateData->getStateName();
					}
					$cityData = $this->cities_entity->init('id',$city_id);
					if($cityData){
						$cityName = $cityData->getCityName(); 
					}
				}
			}
			
			$id = $this->escort_listing_place_options_m->insert(array(
				'country_id'	=> $this->input->post('country_id'),
				'state_id'	=> $this->input->post('state_id'),
				'province_id'	=> $this->input->post('province_id'),
				'city_id'	=> $this->input->post('city_id'),
				'country_name' => $countryName,
				'state_name'	=> $stateName,
				'province_name'	=> $provinceName,
				'city_name'		=> $cityName,
				'price'	=> $this->input->post('price')
			));
			if ($id)
			{
				$this->session->set_flashdata('success', sprintf($this->lang->line('escort_listing_place_add_success')));
			}
			else
			{
				$this->session->set_flashdata('error', $this->lang->line('escort_listing_place_add_error'));
			}
			$this->input->post('btnAction') == 'save_exit' ? redirect('admin/be_listing_place') : redirect('admin/be_listing_place/edit/' . $id);
		}
		else
		{
			// Go through all the known fields and get the post values
			foreach ($this->validation_rules as $key => $field)
			{
				$listingPlace->$field['field'] = set_value($field['field']);
			}
		}

		$this->template
		->title($this->module_details['name'], lang('escort_be_discount_type_add_title'))
		->append_metadata($this->load->view('fragments/wysiwyg', $this->data, TRUE))
		->append_metadata(js('jquery/jquery.tagsinput.min.js'))
		->append_metadata(js('blog_form.js', 'blog'))
		->append_metadata(css('jquery/jquery.tagsinput.css'))
		->set('listingPlace', $listingPlace)
		->build('admin/form', $this->data);
	}
	public function edit($id = 0)
	{
		$id OR redirect('admin/be_listing_place');
		$listingPlace = $this->escort_listing_place_options_m->get($id);
		$listingPlace OR redirect('admin/be_listing_place');
		$this->form_validation->set_rules($this->validation_rules);
		if ($this->form_validation->run())
		{
			$country_id	= $this->input->post('country_id');
			$state_id = $this->input->post('state_id');
			$province_id = $this->input->post('province_id');
			$city_id = $this->input->post('city_id');
			
			$countryData = $this->_ci->countries_entity->init('id',$country_id);
			if($countryData){
				$countryName = $countryData->getCountryName();
			}else{
				$countryName = NULL;
			}
			$stateName = NULL;
			$provinceName = NULL;
			$cityName = NULL;
			if($countryData){
				if($countryData->getCountryCode() == "ES"){
					$stateData =  $this->_ci->states_spain_entity->init('id',$state_id);
					if($stateData){
						$stateName = $stateData->getRegionNameSpanish();
					}
					$provinceData = $this->provinces_spain_entity->init('id',$province_id);
					if($provinceData){
						$provinceName = $provinceData->getProvinceNameSpanish(); 
					}
					$cityData = $this->cities_spain_entity->init('id',$city_id);
					if($cityData){
						$cityName = $cityData->getCityNameSpanish(); 
					}
				}else{
					$stateData =  $this->_ci->states_entity->init('id',$state_id);
					if($stateData){
						$stateName = $stateData->getStateName();
					}
					$cityData = $this->cities_entity->init('id',$city_id);
					if($cityData){
						$cityName = $cityData->getCityName(); 
					}
				}
			}
			$result = $this->escort_listing_place_options_m->update($id, array(
					'country_id'	=> $this->input->post('country_id'),
					'state_id'	=> $this->input->post('state_id'),
					'province_id'	=> $this->input->post('province_id'),
					'city_id'	=> $this->input->post('city_id'),
					'country_name' => $countryName,
					'state_name'	=> $stateName,
					'province_name'	=> $provinceName,
					'city_name'		=> $cityName,
					'price'	=> $this->input->post('price')
			));

			if ($result)
			{
				$this->session->set_flashdata(array('success' => sprintf(lang('escort_listing_place_edit_success'))));
			}
			else
			{
				$this->session->set_flashdata('error', $this->lang->line('escort_listing_place_edit_error'));
			}

			// Redirect back to the form or main page
			$this->input->post('btnAction') == 'save_exit' ? redirect('admin/be_listing_place') : redirect('admin/be_listing_place/edit/' . $id);
		}

		// Go through all the known fields and get the post values
		foreach ($this->validation_rules as $key => $field)
		{
			if (isset($_POST[$field['field']]))
			{
				$discount->$field['field'] = set_value($field['field']);
			}
		}

		$this->template
		->title($this->module_details['name'], sprintf(lang('escort_listing_place_edit_title')))
		->append_metadata($this->load->view('fragments/wysiwyg', $this->data, TRUE))
		->append_metadata(js('jquery/jquery.tagsinput.min.js'))
		->append_metadata(js('blog_form.js', 'blog'))
		->append_metadata(css('jquery/jquery.tagsinput.css'))
		->set('listingPlace', $listingPlace)
		->build('admin/form');
	}
	public function delete($id = 0)
	{
		// Delete one
		$ids = ($id) ? array($id) : $this->input->post('action_to');

		// Go through the array of slugs to delete
		if ( ! empty($ids))
		{
			$listingOptionCodes = array();
			foreach ($ids as $id)
			{
				// Get the current page so we can grab the id too
				if ($listingOption = $this->escort_listing_place_options_m->get($id))
				{
					$this->escort_listing_place_options_m->delete($id);

					// Wipe cache for this model, the content has changed
					$this->pyrocache->delete('escort_listing_place_options_m');
					$listingOptionCodes[] = $listingOption->id;
				}
			}
		}

		// Some pages have been deleted
		if ( ! empty($listingOptionCodes))
		{
			// Only deleting one page
			if (count($listingOptionCodes) == 1)
			{
				$this->session->set_flashdata('success', sprintf($this->lang->line('escort_listing_place_delete_success')));
			}
			// Deleting multiple pages
			else
			{
				$this->session->set_flashdata('success', sprintf($this->lang->line('escort_listing_place_mass_delete_success')));
			}
		}
		// For some reason, none of them were deleted
		else
		{
			$this->session->set_flashdata('notice', lang('escort_listing_place_delete_error'));
		}

		redirect('admin/be_listing_place');
	}
	
	public function get_state_data(){
		$country_id = $this->input->post('country_id');
		$result = array();
		$optionList = '<option value="0">'.lang("escort_listing_place_all_state_label").'</option>';
		if($country_id == '0'){
			$result['spain'] = false;
			$result['data'] = $optionList;
			echo json_encode($result);
			return;
		}
		$ci = &get_instance();
		$current_dbprefix = $ci->db->dbprefix;
		$ci->db->set_dbprefix('default_');
		
		$COUNTRY = $ci->countries_entity->init('id',$country_id);
		$ci->db->set_dbprefix($current_dbprefix);
		
		if($COUNTRY->getCountryCode() == 'ES'){
			$result['spain'] = true;
		}else{
			$result['spain'] = false;
		}
		
		$stateList = getStateData_ioc($country_id);
		if($stateList){
			foreach ($stateList as $key =>$value){
				$optionList .= 	'<option value="'.$key.'">'.utf8_encode($value).'</option>';
			}
		}
		$result['data'] = $optionList;
		echo json_encode($result);
	}
	public function get_city_data(){
		$country_id = $this->input->post('country_id');
		$state_id = $this->input->post('state_id');
		$result = array();
		$optionList = '<option value="0">'.lang("escort_listing_place_all_city_label").'</option>';	
		if($state_id == '0'){
			$result['spain'] = false;
			$result['data'] = $optionList;
			echo json_encode($result);
			return;
		}
		$ci = &get_instance();
		$current_dbprefix = $ci->db->dbprefix;
		$ci->db->set_dbprefix('default_');
		
		$COUNTRY = $ci->countries_entity->init('id',$country_id);
		$ci->db->set_dbprefix($current_dbprefix);
		
		if($COUNTRY->getCountryCode() == 'ES'){
			$cityList = getProvinceData_ioc($country_id, $state_id);
			$result['spain'] = true;
			$optionList = '<option value="0">'.lang("escort_listing_place_all_province_label").'</option>';	
		}else{
			$result['spain'] = false;
			$cityList = getCityData_ioc($country_id, $state_id, 0);
			$optionList = '<option value="0">'.lang("escort_listing_place_all_city_label").'</option>';	
		}

		if($cityList){
			foreach ($cityList as $key =>$value){
				$optionList .= 	'<option value="'.$key.'">'.$value.'</option>';
			}
		}
		$result['data'] = $optionList; 
		echo json_encode($result);
	}
	public function get_spain_city_data(){
		$country_id = $this->input->post('country_id');
		$state_id = $this->input->post('state_id');
		$province_id = $this->input->post('province_id');
		$result = array();
		$optionList = '<option value="0">'.lang("escort_listing_place_all_city_label").'</option>';	
		if($province_id == '0'){
			$result['spain'] = false;
			$result['data'] = $optionList;
			echo json_encode($result);
			return;
		}
		$cityList = getCityData_ioc($country_id, $state_id, $province_id);
		if($cityList){
			foreach ($cityList as $key =>$value){
				$optionList .= 	'<option value="'.$key.'">'.$value.'</option>';
			}
		}
		$result['data'] = $optionList;
		echo json_encode($result);
	}
	public function update(){
		$listingPlaces = $this->escort_listing_place_options_m->get_all();
			
		if($listingPlaces){
			foreach ($listingPlaces as $listingPlace){
				$countryData = $this->_ci->countries_entity->init('id',$listingPlace->country_id);
				if($countryData){
					$listingPlace->countryName = $countryData->getCountryName();
				}else{
					$listingPlace->countryName = NULL;
				}
				$listingPlace->stateName = NULL;
				$listingPlace->provinceName = NULL;
				$listingPlace->cityName = NULL;
				if($countryData){
					if($countryData->getCountryCode() == "ES"){
						$stateData =  $this->_ci->states_spain_entity->init('id',$listingPlace->state_id);
						if($stateData){
							$listingPlace->stateName = $stateData->getRegionNameSpanish();
						}
						$provinceData = $this->provinces_spain_entity->init('id',$listingPlace->province_id);
						if($provinceData){
							$listingPlace->provinceName = $provinceData->getProvinceNameSpanish(); 
						}
						$cityData = $this->cities_spain_entity->init('id',$listingPlace->city_id);
						if($cityData){
							$listingPlace->cityName = $cityData->getCityNameSpanish(); 
						}
					}else{
						$stateData =  $this->_ci->states_entity->init('id',$listingPlace->state_id);
						if($stateData){
							$listingPlace->stateName = $stateData->getStateName();
						}
						$cityData = $this->cities_entity->init('id',$listingPlace->city_id);
						if($cityData){
							$listingPlace->cityName = $cityData->getCityName(); 
						}
					}
				}
				
				$result = $this->escort_listing_place_options_m->update($listingPlace->id, array(
					'country_id'	=> $listingPlace->country_id,
					'state_id'	=> $listingPlace->state_id,
					'province_id'	=> $listingPlace->province_id,
					'city_id'	=> $listingPlace->city_id,
					'country_name' => $listingPlace->countryName,
					'state_name'	=> $listingPlace->stateName,
					'province_name'	=> $listingPlace->provinceName,
					'city_name'		=> $listingPlace->cityName,
					'price'	=> $listingPlace->price
				));
			}
		}
	}
}
